package Core;
/*
 *  BitmapFont version 1.0.1
 *  Author: unknown
 *  Edit: Van Tan Tai 2010-2011
 *  Note: demo version, not use
 */
import java.util.Vector;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

public class BitmapFont
{
    class VFont
    {
        public Image imgFont;
        public String stMap = "";
        public int[] cw;
        public int ch;
        public VFont(Image imgFont, String stMap, int[] cw, int ch) {
            this.imgFont = imgFont;
            this.stMap = stMap;
            this.cw = cw;
            this.ch = ch;
        }
    }

    private static Image imgBrown;
    private static VFont currentFont,brownFont;
    private static final String stMap = " 0123456789.,:!?()+-*/#$%abcdefghijklmnopqrstuvwxyzáàảãạăắằẳẵặâấầẩẫậéèẻẽẹêếềểễệíìỉĩịóòỏõọôốồổỗộơớờởỡợúùủũụưứừửữựýỳỷỹỵđABCDEFGHIJKLMNOPQRSTUVWXYZÁÀẢÃẶĂẮẰẲẴẶÂẤẦẨẪẬÉÈẺẼẸÊẾỀỂỄỆÍÌỈĨỊÓÒỎÕỌÔỐỒỔỖỘƠỚỜỞỠỢÚÙỦŨỤƯỨỪỬỮỰÝỲỶỸỴĐ_«»⌐=[];'^&@";
    private static final int[] cw = { 2, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 3, 2, 2, 2, 5, 6, 6, 7, 4, 6, 5, 8, 5, 9, 6, 6, 5, 6, 6, 4, 6, 6, 2, 3, 6, 2, 10, 6, 6, 6, 6, 4, 5, 4, 6, 6, 8, 6, 6, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 6, 7, 6, 6, 6, 6, 6, 6, 6, 6, 7, 6, 7, 6, 6, 3, 2, 2, 4, 2, 6, 6, 6, 6, 6, 6, 6, 6, 7, 6, 6, 7, 7, 7, 7, 7, 7, 6, 6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 6, 6, 6, 6, 6, 7, 7, 6, 6, 7, 5, 5, 7, 7, 4, 5, 6, 5, 9, 6, 7, 6, 7, 7, 6, 6, 7, 6, 10, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 5, 5, 5, 5, 5, 5, 6, 5, 5, 5, 5, 4, 4, 4, 5, 4, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 7, 7, 7, 7, 7, 9, 9, 9, 9, 9, 9, 6, 6, 6, 6, 6, 8 , 6, 9, 9, 8, 6, 4, 4, 2, 3, 5, 8, 9};
    public static final int ch = 13;

    public BitmapFont() {
        try {
            imgBrown = Image.createImage("/font/brown.png");
        }
        catch (Exception e) {
            System.out.println("Font load error!");
        }
        brownFont = new VFont(imgBrown, stMap, cw, ch);
        currentFont = brownFont;
    }

    public static int getHeight() {
        return currentFont.ch;
    }

    public static int getWidthOf(char ch) {
        return stringWidth(String.valueOf(ch));
    }

    public static int stringWidth(String st) {
        int pos;
        int len = 0;
        for (int i = 0; i < st.length(); i++) {
            pos = currentFont.stMap.indexOf(st.charAt(i));
            if (pos == -1) pos = 0;
            len += (currentFont.cw[pos] + 1);
        }
        return len;
    }
    
	private static void drawString(Graphics g, VFont font, String st, int x, int y, int align){
	     int pos;
	     int x1;
	     int len = st.length();
	     if (align == 0)
	         x1 = x;
	     else if (align == 1)
	         x1 = x - stringWidth(st);
	     else
	         x1 = x - (stringWidth(st) >> 1);
	     for (int i = 0; i < len; i++) {
	         pos = font.stMap.indexOf(st.charAt(i));
	         if (pos == -1) pos = 0;
	         if (pos > -1)
	         {
	             g.drawRegion(font.imgFont, 0, pos * font.ch, font.cw[pos], font.ch, 0, x1, y + 1, 20);//y - 1
	         }
	         x1 += font.cw[pos] + 1;
	     }
	}
	
    public static void drawString(Graphics g, String st, int x, int y, int align) {
        drawString(g, currentFont, st, x, y, align);
    }
    
    public static String[] splitStrInLine(String src, int lineWidth) {
        Vector list = new Vector();
        int srclen = src.length();
        String tem = "";
        int start = 0, end = 0;
        while (true){
            while (stringWidth(tem) < lineWidth){
                tem += src.charAt(end);
                if (end == srclen - 1) break;
                end++;
            }
            if (end != srclen - 1 && (src.charAt(end + 1) != ' ')) {
                int endAnyway = end;
                while (true){
                    if (src.charAt(end + 1) == ' ' && src.charAt(end) != ' ') break;
                    if (end == start) break;
                    end--;
                }
                if (end == start) end = endAnyway;
            }
            list.addElement(src.substring(start, end + 1));
            if (end == srclen - 1) break;
            start = end + 1;
            while (start != srclen - 1 && src.charAt(start) == ' ') start++;
            if (start == srclen - 1) break;
            end = start;
            tem = "";
        }
        String[] strs = new String[list.size()];
        for (int i = 0; i < list.size(); i++)
            strs[i] = (String) list.elementAt(i);
        return strs;
    }
}